import { Component, OnDestroy } from '@angular/core';

import { DoubleDashProductService } from './double.dash.product.service';

import { ProductBase } from '../../product/product.base';

import { Util } from '../../../utils/util';

import { Scope } from '../../../utils/emit';

import { AppSettings } from '../../../app.settings';

import { ToastyService } from '../../../modules/ng2-toasty';

@Component({
  selector: 'double-dash-product',
  templateUrl: './double.dash.product.html',
  providers: [DoubleDashProductService]
})

export class DoubleDashProductComponent extends ProductBase {

  brandInfo: any;

  viewToggel: boolean = true;

  param: any = {
    category: '',
    rows: 10,
    start: 0
  };


  // 列表
  public columns: Array<any> = [
    {
      title: '商品',
      name: 'brandName',
      sort: false,
      class: 'productTh'
    },
    {
      title: '标题',
      name: 'categoryName',
      sort: false,
      class: 'titleTh'
    },
    {
      title: '品牌',
      name: 'shopNum',
      sort: false
    },
    {
      title: '销售量',
      name: 'sellAmount',
      sort: true
    },
    {
      title: '预售价',
      name: 'priceYuShou',
      sort: true
    },
    {
      title: '定金',
      name: 'dingJin',
      sort: true
    }
  ];
  constructor(public util: Util, public scope: Scope, public appSettings: AppSettings, public service: DoubleDashProductService, public toastyService: ToastyService) {
    super(service,util,toastyService);
  }


  // 类目 接收pie component cat change
  cat: any = {
    catName: "",
    wholeName: "",
    categoryId: ""
  };

  sellEvent: any;

  ngOnInit() {
    // 如果不是第一次进入
    this.cat = this.appSettings.getAppCat();

    if (this.cat.categoryId != '') {

      if (this.cat.categoryId < 0) {
        // toast
        let toastOptions = {
          title: "",
          msg: "请选择叶子类目",
          showClose: true,
          timeout: 6000,
          theme: 'material'
        };
        // Add see all possible types in one shot
        this.toastyService.info(toastOptions);

        this.viewToggel = false;

        this.vm.table.products.length = 0;

        return;
      }

      this.viewToggel = true;
      this.getHttp(this.param);

    }

    this.sellEvent = this.scope.on('dashborad-all-cur-cat', (data) => {

      console.log("dash shop");
      console.log(data);

      this.cat = data.data;

      if (this.cat.categoryId < 0) {
        // toast
        let toastOptions = {
          title: "",
          msg: "请选择叶子类目",
          showClose: true,
          timeout: 6000,
          theme: 'material'
        };
        // Add see all possible types in one shot
        this.toastyService.info(toastOptions);

        this.vm.table.products.length = 0;

        this.viewToggel = false;

        return;
      }

      if(!this.viewToggel){
        this.viewToggel= true;
      }

      this.param.start = 0;
      this.pageChanged(1);

    });
  }

  ngOnDestroy() {
    if (this.busy) {
      this.busy.unsubscribe();
    }

    if (this.exportBusy) {
      this.exportBusy.unsubscribe();
    }

    if (this.sellEvent) {
      this.sellEvent.unsubscribe();
    }
  }

  date: any;
  getHttp(param) {


    this.param.category = this.cat.categoryId;

    this.vm.table.products.length = 0;

   var res = {"code":0,"msg":"成功","data":{"value":[{"numberId":560824158736,"title":"【预售到手价2188】新款Apple iPad 9.7英寸苹果平板电脑32G","picUrl":"//img.alicdn.com/imgextra/i3/2616970884/TB1SX5KXAfb_uJkHFJHXXb4vFXa_!!0-item_pic.jpg","loc":"浙江绍兴","propertyValue":"网络类型:WLAN,品牌:Apple/苹果,套餐类型:官方标配,售后服务:全国联保,型号:Ipad,存储容量:32GB,颜色分类:金色 深空灰色 银色,保修期:12个月","brandName":"apple/苹果","sellAmount":40311,"priceYuShou":2388.0,"dingJin":100.0,"diKou":300.0,"category":50019780,"uid":2616970884,"modifyTime":1510048885000},{"numberId":560825350079,"title":"【预售到手价2888】新款Apple iPad 9.7英寸苹果平板电脑128G","picUrl":"//img.alicdn.com/imgextra/i3/2616970884/TB10RO7bsLJ8KJjy0FnXXcFDpXa_!!0-item_pic.jpg","loc":"浙江绍兴","propertyValue":"网络类型:WLAN,品牌:Apple/苹果,套餐类型:官方标配,售后服务:全国联保,型号:Ipad,存储容量:128GB,颜色分类:金色 深空灰色 银色,保修期:12个月","brandName":"apple/苹果","sellAmount":33337,"priceYuShou":3088.0,"dingJin":100.0,"diKou":300.0,"category":50019780,"uid":2616970884,"modifyTime":1510048885000},{"numberId":560663700300,"title":"【预售到手价2718】 苹果iPad mini4 128GB 7.9英寸平板电脑","picUrl":"//img.alicdn.com/imgextra/i1/2616970884/TB11KkkbtbJ8KJjy1zjXXaqapXa_!!0-item_pic.jpg","loc":"浙江绍兴","propertyValue":"分辨率:2048 x 1536 分辨率，326 ppi,品牌:Apple/苹果,CPU主频:不详,套餐类型:官方标配,型号:iPad mini 4,存储容量:128GB,屏幕尺寸:7.9 英寸 (对角线) LED 背光 Multi-Touch 显示屏,处理器型号信息:不详,处理器构架:不详,后置摄像头像素:800 万像素 iSight 摄像头,网络类型:WLAN,上市时间:2016年9月,售后服务:全国联保,电信设备进网许可证编号:不详,颜色分类:金色 深空灰色 银色,操作系统:iOS 9,内存容量:不详,保修期:12个月","brandName":"apple/苹果","sellAmount":17284,"priceYuShou":2918.0,"dingJin":100.0,"diKou":300.0,"category":50019780,"uid":2616970884,"modifyTime":1510048885000},{"numberId":555829941003,"title":"【预定减200】华为荣耀 畅玩平板2(8英寸)4G可通话平板电脑安卓","picUrl":"//img.alicdn.com/imgextra/i1/1114511827/TB11rnYmfBNTKJjSszcXXbO2VXa_!!0-item_pic.jpg","loc":"广东深圳","propertyValue":"分辨率:1280*800,品牌:荣耀,CPU主频:1.4GHz,套餐类型:官方标配,型号:畅玩平板2(8英寸),存储容量:32GB,屏幕尺寸:8.0英寸,处理器型号信息:Qualcomm SnapDragon 425,处理器构架:ARM架构,后置摄像头像素:500万,生产企业:华为,网络类型:4G,上市时间:2017年5月,核心数:四核心,电信设备进网许可证编号:无,颜色分类:前白后日晖金,内存容量:2GB 3GB,接口类型:USB,保修期:12个月","brandName":"荣耀","sellAmount":2035,"priceYuShou":1299.0,"dingJin":100.0,"diKou":300.0,"category":50019780,"uid":1114511827,"modifyTime":1510048910000},{"numberId":559175599664,"title":"Voyo i8pro 10.1英寸4G上网通话八核游戏超薄智能平板电脑安卓","picUrl":"//img.alicdn.com/imgextra/i3/744184646/TB2dZ9mnHsTMeJjSszgXXacpFXa_!!744184646.jpg","loc":"广东深圳","propertyValue":"型号:i8pro,存储容量:64GB,屏幕尺寸:10.1英寸,热门功能:电话 重力感应 蓝牙,厂商:联发科技,生产企业:深圳市永佳合一科技发展有限公司,网络类型:4G,上市时间:2017.09,售后服务:全国联保,核心数:八核心,颜色分类:前白后金色 前白后玫瑰金,内存容量:3GB,接口类型:音频接口 SIM卡 OTG,分辨率:1920x1200,品牌:Voyo,CPU主频:4×1.3GHz+4 ×1.5GHz,套餐类型:官方标配,是否PC平板二合一:否,处理器型号信息:MT6753,处理器构架:ARM Cortex A53,后置摄像头像素:500万,电信设备进网许可证编号:2017CP4499,操作系统:Android 6.0,保修期:24个月,像素:200万,触摸屏类型:电容屏","brandName":"voyo","sellAmount":711,"priceYuShou":899.0,"dingJin":100.0,"diKou":150.0,"category":50019780,"uid":744184646,"modifyTime":1510232855000},{"numberId":556669273077,"title":"Teclast/台电 P10 八核平板电脑智能安卓10.1英寸大屏WIFI超薄pad","picUrl":"//img.alicdn.com/imgextra/i1/1942167737/TB2rnh4aMDD8KJjy0FdXXcjvXXa_!!1942167737.jpg","loc":"上海","propertyValue":"存储类型:EMMC,型号:P10 八核,存储容量:32GB,屏幕尺寸:10.1英寸,热门功能:重力感应 蓝牙,厂商:瑞芯微,生产企业:广州商科信息科技有限公司,网络类型:WIFI,上市时间:2017年8月,售后服务:全国联保,核心数:八核心,颜色分类:前白后银,内存容量:2GB,接口类型:音频输入 音频输出 闪存卡读取,处理器品牌:瑞芯微,分辨率:1920x1200,品牌:Teclast/台电,CPU主频:1.5GHz,套餐类型:出厂标配 官方标配,是否PC平板二合一:否,处理器型号信息:RK3368H,处理器构架:Coretex-A53,后置摄像头像素:500万,电信设备进网许可证编号:无,操作系统:android 7.1,保修期:12个月,像素:200万,触摸屏类型:电容屏","brandName":"teclast/台电","sellAmount":687,"priceYuShou":689.0,"dingJin":100.0,"diKou":150.0,"category":50019780,"uid":1942167737,"modifyTime":1510231995000},{"numberId":552535034704,"title":"Huawei/华为 平板 M3 青春版10英寸电脑安卓智能4G通话全网通手机","picUrl":"//img.alicdn.com/imgextra/i4/385131841/TB1Q4KZbv2H8KJjy0FcXXaDlFXa_!!0-item_pic.jpg","loc":"广东广州","propertyValue":"分辨率:1920x1200,品牌:Huawei/华为,CPU主频:4x1.4GHz + 4x1.1GHz,套餐类型:官方标配,是否PC平板二合一:否,型号:平板 M3 青春版10英寸,存储容量:64GB 32GB,屏幕尺寸:10.1英寸,处理器型号信息:MSM8940,热门功能:加速传感器，指南针，状态指示灯 重力感应 光线感应,处理器构架:64-bit ARM Cortex A53,后置摄像头像素:800万,生产企业:华为,网络类型:全网通 WIFI,上市时间:2017年6月,售后服务:全国联保,核心数:八核心,电信设备进网许可证编号:2017AP2051,颜色分类:皎月白（3GB） 苍穹灰（3GB） 流光金（4GB） 苍穹灰（4GB）,操作系统:android 7.0,内存容量:3GB 4GB,接口类型:USB OTG,保修期:12个月,触摸屏类型:电容屏","brandName":"huawei/华为","sellAmount":681,"priceYuShou":1849.0,"dingJin":100.0,"diKou":200.0,"category":50019780,"uid":385131841,"modifyTime":1510048899000},{"numberId":556782634713,"title":"【双11预售 定金100最高抵300】Huawei/华为 M3平板电脑3D立体音","picUrl":"//img.alicdn.com/imgextra/i2/2838892713/TB1pKQCq8USMeJjy1zkXXaWmpXa_!!0-item_pic.jpg","loc":"广东深圳","propertyValue":"分辨率:2560x1600,品牌:Huawei/华为,CPU主频:4x2.3GHz + 4x1.8GHz,套餐类型:官方标配,型号:M3平板电脑,存储容量:128GB 64GB 32GB,屏幕尺寸:8.4英寸,处理器型号信息:海思麒麟950,热门功能:指纹导航、影音锁屏、学生模式、护眼模式、双任务窗口、华为视频,处理器构架:麒麟950,生产企业:惠州光弘科技股份有限公司,网络类型:WIFI,核心数:八核心,电信设备进网许可证编号:17-5043-162863,颜色分类:前白后日晖金,操作系统:android 7.0,内存容量:4GB,保修期:12个月,像素:800万,触摸屏类型:电容屏","brandName":"huawei/华为","sellAmount":596,"priceYuShou":1888.0,"dingJin":100.0,"diKou":200.0,"category":50019780,"uid":2838892713,"modifyTime":1510048975000},{"numberId":552865939083,"title":"Microsoft/微软 Surface Pro M 4G 128G New 平板电脑二合一","picUrl":"//img.alicdn.com/imgextra/i4/2662762851/TB2uPCjahrI8KJjy0FpXXb5hVXa_!!2662762851.jpg","loc":"上海","propertyValue":"处理器品牌:Intel/英特尔,品牌:Microsoft/微软,CPU主频:1000MHz,套餐类型:官方标配,型号:Surface Pro M 4G 128G,存储容量:128GB,处理器型号信息:i5-6300U,处理器构架:Skylake,生产企业:微软（中国）有限公司,网络类型:WIFI,售后服务:全国联保,电信设备进网许可证编号:无,颜色分类:银灰色,内存容量:4GB,保修期:24个月","brandName":"microsoft/微软","sellAmount":382,"priceYuShou":5488.0,"dingJin":100.0,"diKou":400.0,"category":50019780,"uid":2662762851,"modifyTime":1510048897000},{"numberId":558890713994,"title":"酷比魔方 KNote 11.6英寸128GBwin10家用商务二合一平板电脑","picUrl":"//img.alicdn.com/imgextra/i3/714625138/TB2IsZBXMnH8KJjSspcXXb3QFXa_!!714625138.jpg","loc":"广东广州","propertyValue":"型号:KNOTe,存储容量:128GB,屏幕尺寸:11.6英寸,热门功能:重力感应 原笔迹手写 可拆卸键盘 蓝牙,厂商:英特尔,生产企业:深圳市欧度利方科技有限公司,网络类型:WIFI,上市时间:2017年9月下旬,售后服务:全国联保,核心数:四核心,颜色分类:前黑后灰,内存容量:6GB,接口类型:Type-C(数据传输、充电、视频输出)，DC充电接口、Dock键盘接口 音频输出 闪存卡读取,处理器品牌:Intel/英特尔,分辨率:1920x1080,品牌:酷比魔方,CPU主频:主频1.1GHz（睿频峰值2.2Ghz),套餐类型:官方标配 套餐一 套餐二 套餐三 套餐四 套餐五,是否PC平板二合一:是,处理器型号信息:N3450,处理器构架:Apollolake,后置摄像头像素:500万,电信设备进网许可证编号:无,操作系统:windows 10,保修期:12个月,像素:200万,触摸屏类型:电容屏","brandName":"酷比魔方","sellAmount":352,"priceYuShou":2199.0,"dingJin":100.0,"diKou":150.0,"category":50019780,"uid":714625138,"modifyTime":1510248553000}],"count":24}};

      this.vm.table.products = res.data.value;

      this.vm.table.count = res.data.count;

      if (this.vm.table.products.length > 0) {
        this.date = this.vm.table.products[0].modifyTime;
      }

  }


  export() {


    let dim = ['modifyTime', 'numberId', 'title', 'picUrl', 'loc', 'propertyValue', 'brandName', 'sellAmount', 'priceYuShou', 'dingJin', 'diKou', 'uid'];

    let dimText = ['时间', '宝贝Id', '宝贝标题', '宝贝图片链接', '地址', '属性', '品牌名', '销售量', '预售价', '定金', '抵扣', '店铺id'];

    let fileName = this.cat.wholeName + "全部预售宝贝";
    this.productExport(this.param,dim,dimText,fileName);
  }

}
